package com.lyn.pluginbazaar.service;

import com.alibaba.fastjson.JSONObject;
import com.lyn.pluginbazaar.api.UserAdapter;
import com.lyn.pluginbazaar.dao.user.User;
import com.lyn.pluginbazaar.dao.user.UserDao;
import com.lyn.pluginbazaar.entity.LoginUser;
import com.lyn.pluginbazaar.entity.PagingQuery;
import com.lyn.pluginbazaar.entity.Result;
import com.lyn.pluginbazaar.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Service
public class UserService implements UserAdapter {

    @Autowired
    private UserDao userDao;


    @Override
    public Result login(LoginUser loginUser, HttpServletRequest request, HttpServletResponse response) {
        String check = CheckUtil.check(loginUser);
        if (!CheckUtil.isEmpty(check)) {
            return Result.FAIL(check);
        }
        if (CheckUtil.isEmpty(loginUser.getCheckKey())) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("checkKey")) {
                        loginUser.setCheckKey(cookie.getValue());
                    }
                }
            } else {
                return Result.FAIL("验证码错误!");
            }
        }
        String checkCode = (String) RedisUtil.getInstance().get(loginUser.getCheckKey());
        if (CheckUtil.isEmpty(checkCode)) {
            return Result.FAIL("验证码错误!");
        }
        if (!checkCode.equals(loginUser.getCheckCode())) {
            return Result.FAIL("验证码错误!");
        }
        String password = DesUtil.getInstance().encrypt(loginUser.getPassword());
        User byUserName = userDao.findByUserName(loginUser.getUserName());
        if (byUserName == null) {
            return Result.FAIL("用户不存在!");
        }
        if (!password.equals(byUserName.getPassword())) {
            return Result.FAIL("密码错误!");
        }
        String token = TokenUtil.getToken(byUserName);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", token);
        TokenUtil.setToken(response, token);
        RedisUtil.getInstance().set(token, token, 60 * 60 );
        return Result.SUCCESS("登录成功!", jsonObject);
    }

    @Override
    public Result logOut(HttpServletRequest request, HttpServletResponse response) {
        return Result.SUCCESS("注销成功!");
    }

    @Override
    public Result getCheckCode(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> imgCodeBaseCode = CheckCodeUtil.getImgCodeBaseCode(4);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("base64", imgCodeBaseCode.get("base64"));
        String checkKey = UUidUtil.getUid();
        jsonObject.put("checkKey", checkKey);
        boolean imgCode = RedisUtil.getInstance().set(checkKey, imgCodeBaseCode.get("imgCode"), 60);
        if (!imgCode) {
            return Result.FAIL("获取失败!");
        }
        response.addCookie(new Cookie("checkKey", checkKey));
        return Result.SUCCESS("获取成功!", jsonObject);
    }

    @Override
    public Result checkLogin(HttpServletRequest request, HttpServletResponse response) {
        return Result.SUCCESS("已经登录成功!");
    }

    @Override
    public Result pagingQuery(PagingQuery pagingQuery) {

        return null;
    }

    @Override
    public Result count(PagingQuery pagingQuery) {
        long count = JpaUtil.count(userDao, User.class, pagingQuery);
        return Result.SUCCESS("查询成功!", count);
    }
}
